package com.codeoss.framework.view.renderer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.codeoss.framework.context.YunContext;
import com.codeoss.framework.converter.IConverter;
import com.codeoss.framework.converter.IConverterStore;
import com.codeoss.framework.expression.Expression;
import com.codeoss.framework.expression.IScriptService;
import com.codeoss.framework.expression.IScriptServiceStore;
import com.codeoss.framework.view.component.Component;
import com.codeoss.framework.view.component.Page;
import com.codeoss.framework.view.component.Time;
import com.codeoss.framework.view.service.IVariableService;
import com.codeoss.framework.view.util.DependObject;

@Service
public class TimeRenderer extends CommonComponentRenderer {
	@Autowired
	private IScriptServiceStore ScriptServiceStore;
	@Autowired
	private IVariableService variableService;
	@Autowired
	private IConverterStore converterStore;
	protected void startRenderView(YunContext yunContext, Component component, ViewSerializeMap viewSerializeMap) {
		super.startRenderView(yunContext, component, viewSerializeMap);
		Time time = (Time) component;
		viewSerializeMap.put("col", time.getCol());
		viewSerializeMap.put("fieldcol", time.getFieldCol());
		viewSerializeMap.put("noLabel", time.isNoLabel());
		viewSerializeMap.put("dataField", time.getDataField());
		Expression labelExpression = time.getLabelExpression();
		if (labelExpression != null) {
			IScriptService scriptService=ScriptServiceStore.getScriptService(labelExpression);
			Object value = scriptService.eval(labelExpression.getScript(), yunContext.getVarValueMap());
			viewSerializeMap.put("label", value);
		}
		Page page =(Page)yunContext.getPage();
		DependObject dependObject = page.getDependObject();
		if (dependObject.getDependList(component.getId()) != null) {
			viewSerializeMap.put("componentValueChangeSystem", true);
		}
		if(time.getOnChangeExpression()!=null) {
			viewSerializeMap.put("onChange", true);
		}
	}
	
	@Override
	protected PropSerializeMap startRenderProp(YunContext yunContext, Component component, ViewPropMap viewPropMap) {
		PropSerializeMap propSerializeMap = super.startRenderProp(yunContext, component, viewPropMap);
		Time time = (Time) component;
		
		Object value=variableService.getVariableValue(yunContext, time.getVarName());
		if(value!=null) {
			IConverter converter=converterStore.getConverter("time");
			value=converter.convertObjectToString(value);
		}
		propSerializeMap.put("value", value);
		
		if(yunContext.getAttribute(time.getId()+"_visible")!=null) {
			propSerializeMap.put("visible", yunContext.getAttribute(time.getId()+"_visible"));
		}
		
		if(yunContext.getAttribute(time.getId()+"_readonly")!=null) {
			propSerializeMap.put("readonly", yunContext.getAttribute(time.getId()+"_readonly"));
		}
		return propSerializeMap;
	}
}
